﻿using System;
using SmartHRMS.Infrastructure;
using StructureMap;
using System.Collections.Generic;

namespace SmartHRMS.Model.Account
{
    public class Role : BusinessBase<Role>, IAggregateRoot, IEntity
    {
        #region Properties

        public Guid RoleId { get; private set; }

        public string Name { get; set; }

        public CommonFields CommonFields { get; set; }

        private ICollection<Permission> permission = new HashSet<Permission>();

        public Mode Mode { get; set; }

        private IRoleService _IRoleService;

        #endregion

        public Role()
        {
        }

        public Role(IRoleService _IRoleService)
        {
            this._IRoleService = _IRoleService;
        }

        public ICollection<Permission> Permission 
        {
            get { return permission; }

            set { permission = value; }

        }

        public override void validate()
        {
            if (string.IsNullOrEmpty(this.Name))
            {
                this.BrokenRules.Add(new BrokenRule("Name", "Role name is a required value"));
            }
            else
            {
                if (this.Name.Length > 50)
                {
                    this.BrokenRules.Add(new BrokenRule("Name", "Role name should be 50 chars or less"));
                }
                else
                {
                    if (this.Mode != Mode.Edit)
                    {
                        _IRoleService = ObjectFactory.GetInstance<IRoleService>();

                        if (_IRoleService.IsRoleNameExist(this.Name) == true)
                        {
                            this.BrokenRules.Add(new BrokenRule("Name", "Role name aready exists please try other one."));
                        }
                    }
                }
            }
        }
    }
}
